﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SpriteClasses;

namespace FrostFights
{
    public class ParticleGenerator
    {
        Texture2D texture;
        float spawnWidth;
        float density;

        List<Snowfall> rainDrops = new List<Snowfall> { };

        float timer;

        Random rand;
        public ParticleGenerator(Texture2D newTexture, float newSpawnWidth, float newDensity)
        {
            texture = newTexture;
            spawnWidth = newSpawnWidth;
            density = newDensity;
            rand = new Random();
        }

        public void createParticle()
        {
            float offset = 250.0f;

            rainDrops.Add(new Snowfall(texture, new Vector2(
                -offset + (float)rand.NextDouble() * (spawnWidth + offset), 0),
                new Vector2(1, rand.Next(2, 4))));
        }

        public void Update(GameTime gameTime, GraphicsDevice graphics)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            while (timer > 0)
            {
                timer -= 1f / density;
                createParticle();
            }

            for (int i = 0; i < rainDrops.Count; i++)
            {
                rainDrops[i].Update();

                if (rainDrops[i].Position.Y > graphics.Viewport.Height)
                {
                    rainDrops.RemoveAt(i);
                    i--;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Snowfall drop in rainDrops)
                drop.Draw(spriteBatch);
        }
    }
}
